<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:da="livcos.org/ns/cosmos/data-access"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="c da f xs">

  <xsl:import href="../style/base.xsl" />
  <xsl:import href="../style/web.xsl" />
  
  <xsl:variable name="browseUri" select="'cosmos.view'"/>
  
  <xsl:param name="objId"/>

  <xsl:variable name="adminObjId">
    <xsl:value-of select="$objId"/>
    <xsl:if test="normalize-space($objId) = ''">/cosmos</xsl:if>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:apply-templates select="document(concat($adminObjId,'.meta2'))/*"/>
  </xsl:template>

  <xsl:template match="*" mode="label">
    <p>
      <a href="javascript:loadObject('{$toRootPath}', '{$objId}', 'meta2')">
        <xsl:value-of select="name(.)" />
      </a>
      <xsl:apply-templates select="." mode="details"/>
    </p>
  </xsl:template>
  
  <xsl:template match="da:folder" mode="label">
    <p class="da_label">
      <xsl:text>Folder Access </xsl:text>
      <xsl:apply-templates select="." mode="details"/>
    </p>
  </xsl:template>

  <xsl:template match="da:file" mode="label">
    <p class="da_label">
      <xsl:text>File Access </xsl:text>
      <xsl:apply-templates select="." mode="details"/>
    </p>
  </xsl:template>

  <xsl:template match="c:*" mode="label">
    <xsl:variable name="id">
      <xsl:if test="$objId != '/cosmos'"><xsl:value-of select="$objId"/></xsl:if>
      <xsl:text>/</xsl:text>
      <xsl:value-of select="local-name()"/>
    </xsl:variable>
    <p id="{$id}.meta2">
      <xsl:choose>
        <xsl:when test="c:* or da:folder">
          <xsl:attribute name="class">folder</xsl:attribute>
          <img src="../img/folder.png"/>
          <xsl:text> </xsl:text>
          <xsl:call-template name="buildNavLink">
            <xsl:with-param name="label" select="name(.)"/>
            <xsl:with-param name="objId" select="$id"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="class">object</xsl:attribute>
          <img src="../img/readMode.png"/>
          <xsl:text> </xsl:text>
          <a href="javascript:loadObject('{$toRootPath}', '{$id}', 'cosmos')">
            <xsl:value-of select="name(.)" />
          </a>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:apply-templates select="." mode="details"/>
    </p>
  </xsl:template>

  <xsl:template match="*" mode="details">
    <span class="details"> (<xsl:value-of select="namespace-uri(.)" />)</span>
  </xsl:template>

  <xsl:template match="da:*" mode="details">
    <span class="details">
      <xsl:text> (</xsl:text>
      <xsl:variable name="textLen" select="string-length(@path)"/>
      <xsl:choose>
        <xsl:when test="$textLen &gt; 23">
          <xsl:text>...</xsl:text>
          <xsl:value-of select="substring(@path, $textLen - 20, $textLen)" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@path" />
        </xsl:otherwise>
      </xsl:choose>
      <xsl:text>)</xsl:text>
    </span>
  </xsl:template>

  <xsl:template match="c:*" mode="details">
  </xsl:template>

  <xsl:template match="*" mode="count">
    <xsl:param name="i"/>
    <xsl:param name="count"/>
    <xsl:choose>
      <xsl:when test="not(following-sibling::*[1])">
        <xsl:value-of select="$count + 1"/>
      </xsl:when>
      <xsl:when test="$i > 1">
        <xsl:apply-templates select="following-sibling::*[1]" mode="count">
          <xsl:with-param name="i" select="$i - 1"/>
          <xsl:with-param name="count" select="$count + 1"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$count + 1"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="da:*" mode="count">
    <xsl:param name="i"/>
    <xsl:param name="count"/>
    <xsl:choose>
      <xsl:when test="not(following-sibling::*[1])">
        <xsl:value-of select="$count + 1"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="following-sibling::*[1]" mode="count">
          <xsl:with-param name="i" select="($i - 1) + current()/@count"/>
          <xsl:with-param name="count" select="$count + 1"/>
        </xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="*">
    <xsl:param name="count"/>
    <xsl:apply-templates select="." mode="label"/>
    <xsl:choose>
      <xsl:when test="not($count)">
        <xsl:apply-templates select="following-sibling::*[1]"/>
      </xsl:when>
      <xsl:when test="$count > 1">
        <xsl:apply-templates select="following-sibling::*[1]">
          <xsl:with-param name="count" select="$count - 1"/>
        </xsl:apply-templates>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="da:*">
    <xsl:param name="count"/>
    <xsl:variable name="numNodes">
      <xsl:apply-templates select="following-sibling::*[1]" mode="count">
        <xsl:with-param name="i" select="current()/@count"/>
        <xsl:with-param name="count" select="0"/>
      </xsl:apply-templates>
    </xsl:variable>
    <div class="da_block" id="1.{count(preceding-sibling::node())+1}">
      <xsl:apply-templates select="." mode="label"/>
      <xsl:if test="(normalize-space(@count) != '') and (@count > 0)">
        <xsl:apply-templates select="following-sibling::*[1]">
          <xsl:with-param name="count" select="current()/@count"/>
        </xsl:apply-templates>
      </xsl:if>
    </div>
    <xsl:choose>
      <xsl:when test="not($count) and (normalize-space($numNodes) != '')">
        <xsl:apply-templates select="following-sibling::*[position() = ($numNodes + 1)]"/>
      </xsl:when>
      <xsl:when test="not($count)">
        <xsl:apply-templates select="following-sibling::*[1]"/>
      </xsl:when>
      <xsl:when test="$count > 1">
        <xsl:apply-templates select="following-sibling::*[position() = ($numNodes + 1)]">
          <xsl:with-param name="count" select="$count - 1"/>
        </xsl:apply-templates>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="c:*" mode="navPath">
    <xsl:call-template name="buildNavPath">
      <xsl:with-param name="baseUri" select="''"/>
      <xsl:with-param name="path" select="substring(f:object-id(.),2)"/>
    </xsl:call-template>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="name(.)"/>
  </xsl:template>

  <xsl:template match="/c:*">
    <html id="{$viewObjId}">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Cosmos Admin</title>
        <link href="../css/styles.css" rel="stylesheet" type="text/css" />
        <link href="../css/edit.css" rel="stylesheet" type="text/css" />
        <link href="../css/cosmos_admin.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="../script/util.js"><!-- --></script>
        <script type="text/javascript" src="../script/E.js"><!-- --></script>
        <script type="text/javascript" src="../script/init.js"><!-- --></script>
        <script type="text/javascript" src="../script/gui.js"><!-- --></script>
        <script type="text/javascript" src="../script/DragDrop.js"><!-- --></script>
        <script type="text/javascript" src="../script/doc.js"><!-- --></script>
        <script type="text/javascript" src="../script/Entity.js"><!-- --></script>
        <script type="text/javascript" src="../script/EntityView.js"><!-- --></script>
        <script type="text/javascript" src="../script/editActions.js"><!-- --></script>
        <script type="text/javascript" src="../script/EditJob.js"><!-- --></script>
        <script type="text/javascript" src="../script/EditorPanel.js"><!-- --></script>
        <script type="text/javascript" src="../script/Selection.js"><!-- --></script>
        <script type="text/javascript" src="../script/edit.js"><!-- --></script>
        <script type="text/javascript" src="../script/cosmos.js"><!-- --></script>
        <xsl:call-template name="headExtension"/>
      </head>
      <body>
        <div id="{f:object-id(.)}.meta" class="contentPanel">
          <div class="header_panel">
            <div class="title">
              <xsl:text>/</xsl:text>
              <xsl:call-template name="buildNavLink">
                <xsl:with-param name="label" select="'cosmos'"/>
                <xsl:with-param name="objId" select="'/cosmos'"/>
                <xsl:with-param name="preview" select="false()"/>
              </xsl:call-template>
              <xsl:if test="f:object-id(.) != '/cosmos'">
                <xsl:apply-templates select="." mode="navPath"/>
              </xsl:if>
              <xsl:text> </xsl:text>
              <xsl:variable name="responseId" select="concat('/livcos.org/web/admin/',$browseUri)"/>
              <a href="refresh.job?objId={f:object-id(.)}&amp;responseId={$responseId}">
                <img src="../img/refresh.gif" border="0" alt="refresh"/>
              </a>
              <xsl:text> </xsl:text>
              <a href="clear.job?objId={f:object-id(.)}&amp;responseId={$responseId}">
                <img src="../img/clear.gif" border="0" alt="clear"/>
              </a>
            </div>
          </div>
          <div class="body_panel">
            <div class="nav_panel">
              <xsl:call-template name="buildNavPanel"/>
            </div>
            <div class="main_panel">
              <div id="_content" class="content_panel">
                <xsl:apply-templates select="." mode="content"/>
              </div>
            </div>
          </div>
        </div>
      </body>
    </html>
  </xsl:template>

  <xsl:template name="headExtension">
  </xsl:template>

  <xsl:template name="buildNavLink">
    <xsl:param name="label"/>
    <xsl:param name="objId"/>
    <xsl:param name="preview"/>
    <a href="{$browseUri}?objId={$objId}"><xsl:value-of select="$label"/></a>
  </xsl:template>

  <xsl:template name="buildNavPanel">
    <xsl:apply-templates select="*[1]"/>
  </xsl:template>

  <xsl:template name="buildNavPath">
    <xsl:param name="baseUri"/>
    <xsl:param name="path"/>
    <xsl:variable name="id" select="substring-before($path,'/')"/>
    <xsl:variable name="uri" select="concat($baseUri,'/',$id)"/>
    <xsl:if test="contains($path,'/')">
      <xsl:text>/</xsl:text>
      <xsl:call-template name="buildNavLink">
        <xsl:with-param name="label" select="$id"/>
        <xsl:with-param name="objId" select="$uri"/>
        <xsl:with-param name="preview" select="false()"/>
      </xsl:call-template>
      <xsl:call-template name="buildNavPath">
        <xsl:with-param name="baseUri" select="$uri"/>
        <xsl:with-param name="path" select="substring-after($path,'/')"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="c:*" mode="content">
    <xsl:choose>
      <xsl:when test="contains(substring($objId, 2), '/')">
        <p>The "<xsl:value-of select="$objId"/>" object node.</p>
      </xsl:when>
      <xsl:otherwise>
        <p>The "<xsl:value-of select="$objId"/>" domain node.</p>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="c:cosmos" mode="content">
    <p>The root node of the Cosmos.</p>
    <p>It contains domain object nodes to form the root level.</p>
  </xsl:template>
  
</xsl:stylesheet>
